def get_sum(num):
    s = 0
    for i in range(1, num + 1):
        s += i
    print(f'1到{num}之间的累加和：{s}')


# 调用
get_sum(10)
get_sum(100)


def happy_birthday(name, age):
    print(name, str(age), '岁生日快乐')


def happy_birthday(name='mike', age=30):
    print(name, str(age), '岁生日快乐')


happy_birthday('brucelee', '30')
happy_birthday()


# 个数可变的位置参数
def fun(*para):
    print(type(para))
    for i in para:
        print(i)


fun(1, 2, 34, )
fun([1, 2, 3])
fun(*[1, 2, 3])  # 元组解包


# 个数可变的关键字参数
def fun2(**kwparas):
    print(type(kwparas))
    for key, value in kwparas.items():
        print(key, value)


fun2(name='mike', age=20)
fun2(**{'name': 'mike', 'age': 20})  # 字典解包


# 带返回值的函数
# 返回一个值
def sum(a, b):
    print(a + b)
    return a + b


print(sum(10, 20))


# 返回多个值，类型为元组
def fun3(num):
    return num - 1, num, num + 1


result = fun3(10)
print(type(result))
print(result)

# 解包赋值
a, b, c = result
print(a, b, c)

# 匿名函数lambda
s = lambda a, b: a + b
print(type(s))
print(s(10, 30))

# 列表取值
list = [10, 30, 50, 90]
for i in range(len(list)):
    result = lambda x: x[i]
    print(result(list))

# 字典取值
student_score = [
    {'name': '小明', 'score': 60},
    {'name': '小梁', 'score': 100},
    {'name': '小张', 'score': 90}
]
student_score.sort(key=lambda x: x.get('score'), reverse=True)
print(student_score)


# 递归函数
# n的阶乘
def fac(n):
    if n == 1:
        return 1
    else:
        return n * fac(n - 1)


print(fac(1))
print(fac(3))
print(fac(10))


# 斐波那契数列
def fac1(n):
    if n == 1 or n == 2:
        return 1
    else:
        return fac1(n - 1) + fac1(n - 2)


print(fac1(1))
print(fac1(3))
print(fac1(9))

# 常用的内置函数
